package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.stark.riddle.lib.model.RiddleConst;
import com.stark.riddle.lib.model.RiddlePrefUtil;
import com.stark.riddle.lib.model.bean.Saying;
import com.stark.riddle.lib.model.db.RiddleDaoHelperManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivitySayingBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;

public class SayingActivity extends BaseAc<ActivitySayingBinding> {

    //歇后语
    public static int level = 1;
    private List<Saying> listAll = new ArrayList<>();//全部数据
    private int rightNum;
    private Dialog myRightDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_saying;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivSayingBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rlSayingOptionA.setOnClickListener(this);
        mDataBinding.rlSayingOptionB.setOnClickListener(this);
        mDataBinding.rlSayingOptionC.setOnClickListener(this);
        mDataBinding.rlSayingOptionD.setOnClickListener(this);
        //回答正确弹框
        rightDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.rlSayingOptionA://选项A
                isRight(1);
                break;
            case R.id.rlSayingOptionB://选项B
                isRight(2);
                break;
            case R.id.rlSayingOptionC://选项C
                isRight(3);
                break;
            case R.id.rlSayingOptionD://选项D
                isRight(4);
                break;
            //回答正确弹框
            case R.id.ivDialogRightNext://下一题
                myRightDialog.dismiss();
                passLevel();
                break;
        }
    }

    @Override
    protected void initData() {
        getLevel();
    }

    //获取关卡
    private void getLevel() {
        RiddleDaoHelperManager.getSayingDbHelper().getByKind(SelLevelActivity.kindName, 0, 80, new IRetCallback<List<Saying>>() {
            @Override
            public void onResult(List<Saying> result) {
                if (result != null && result.size() > 0) {
                    listAll.addAll(result);
                    setData();
                }
            }
        });
    }

    //设置游戏数据
    private void setData() {
        mDataBinding.tvSayingLevel.setText(getString(R.string.problem_num, level));
        mDataBinding.tvSayingProblem.setText(listAll.get(level).getSayingDes());
        List<Saying> list = RandomUtil.randomGetItems(listAll, listAll.get(level), 3);
        mDataBinding.tvSayingOptionA.setText(list.get(0).getSayingKey());
        mDataBinding.tvSayingOptionB.setText(list.get(1).getSayingKey());
        mDataBinding.tvSayingOptionC.setText(list.get(2).getSayingKey());
        mDataBinding.tvSayingOptionD.setText(list.get(3).getSayingKey());
        boolean isA = checkAnswer(list.get(0).getSayingKey(), listAll.get(level).getSayingKey());
        boolean isB = checkAnswer(list.get(1).getSayingKey(), listAll.get(level).getSayingKey());
        boolean isC = checkAnswer(list.get(2).getSayingKey(), listAll.get(level).getSayingKey());
        boolean isD = checkAnswer(list.get(3).getSayingKey(), listAll.get(level).getSayingKey());
        if (isA) {
            rightNum = 1;
        }
        if (isB) {
            rightNum = 2;
        }
        if (isC) {
            rightNum = 3;
        }
        if (isD) {
            rightNum = 4;
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }

    //判断对错
    private void isRight(int kind) {
        if (kind == rightNum) {
            myRightDialog.show();
        } else {
            mDataBinding.tvSayingError.setVisibility(View.VISIBLE);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mDataBinding.tvSayingError.setVisibility(View.INVISIBLE);
                }
            }, 500);
        }
    }

    //过关
    private void passLevel() {
        //判断是否需要解锁下一关
        if (level < (listAll.size())) {//如果当前关卡小于总关卡数
            int spLevel = RiddlePrefUtil.getUnlockCheckPoint(RiddleConst.FuncType.SAYING, SelLevelActivity.kindName);//获取记录通过关卡数
            if (level == spLevel) {//如果通过的是未解锁关卡则解锁下一关
                RiddlePrefUtil.saveUnlockCheckPoint(RiddleConst.FuncType.SAYING, SelLevelActivity.kindName, spLevel + 1);
            }
            level++;
        }
        setData();
    }

    //回答正确弹框
    private void rightDialog() {
        myRightDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_right, null);
        myRightDialog.setContentView(inflate);
        myRightDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myRightDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogNext = inflate.findViewById(R.id.ivDialogRightNext);
        dialogNext.setOnClickListener(this);
    }

}